//
//  Set_VC.m
//  Run
//
//  Created by BetaTheta on 16/3/31.
//  Copyright © 2016年 BetaTheta. All rights reserved.
//

#import "Set_VC.h"
#import "CleanCaches.h"

@interface Set_VC ()
@property (nonatomic, strong) UIView *setView;
@property (nonatomic, strong) UILabel *clearLabel;
@property (nonatomic, strong) UIButton *clearButton;
@property (nonatomic, strong) UIImageView *setImage;
@end

@implementation Set_VC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createView];
    
    self.navigationItem.title = @"设置";
    self.view.backgroundColor =[UIColor cyanColor];
    
    


}

- (void)createView {
//    底层View
    self.setView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT/2, WIDTH, HEIGHT/2)];
    _setView.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
    
    [self.view addSubview:_setView];
    
//   清除缓存
    self.clearLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, WIDTH, 30)];
    NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
    
    
    NSString *chacePath = [path stringByAppendingPathComponent:@"Caches"];

    
    _clearLabel.text =
 [NSString stringWithFormat:@"内存:%.2fM", [self folderSizeAtPath:chacePath]];
    _clearLabel.tintColor = [UIColor cyanColor];
    _clearLabel.backgroundColor = [UIColor whiteColor];
    _clearLabel.textAlignment = NSTextAlignmentCenter;
    
    [self.setView addSubview:_clearLabel];
    
//    清除按钮
    self.clearButton = [UIButton buttonWithType:UIButtonTypeSystem];
    
    [_clearButton setTitle:@"清除缓存" forState:UIControlStateNormal];
    _clearButton.frame = CGRectMake(0, 51, self.view.frame.size.width, 30);
    _clearButton.backgroundColor = [UIColor whiteColor];
    _clearButton.tintColor = [UIColor blackColor];
    _clearButton.titleLabel.font = [UIFont systemFontOfSize:15];

    
    [_clearButton addTarget:self action:@selector(clearButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.setView addSubview:_clearButton];

    
//    背景图
    self.setImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/2)];
    _setImage.image = [UIImage imageNamed:@"设置背景图"];
    
    [self.view addSubview:_setImage];
    
}

-(void)cleanAction
{
    //获取完整路径
    NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
    
    NSString *cachePath = [path stringByAppendingPathComponent:@"Caches"];
    NSLog(@"%@", cachePath);
    
    NSFileManager *manage = [NSFileManager defaultManager];
    if ([manage fileExistsAtPath:cachePath]) {
        NSArray *fileNameArray = [manage subpathsAtPath:cachePath];
        for (NSString *fileName in fileNameArray) {
            //拼接绝对路径
            NSString *absolutePath = [cachePath stringByAppendingPathComponent:fileName];
            //通过文件管理者删除文件
            [manage removeItemAtPath:absolutePath error:nil];
        }
    }
    
    //及时更新label上文件的大小
    _clearLabel.text = [NSString stringWithFormat:@"%.2fM", [self folderSizeAtPath:cachePath]];
}

#warning 计算单个文件大小的方法
-(long long)fileSizeAtPath:(NSString *)path
{
    //创建一个文件管理者
    NSFileManager *manage = [NSFileManager defaultManager];
    if ([manage fileExistsAtPath:path]) {
        return [[manage attributesOfItemAtPath:path error:nil] fileSize];
    }
    return 0;
}

#warning 计算一个文件夹大小
-(float)folderSizeAtPath:(NSString *)path
{
    //创建文件管理者
    NSFileManager *manage = [NSFileManager defaultManager];
    if (![manage fileExistsAtPath:path]) {
        return 0;
    }
    //根据路径获取文件夹里面的元素集合
    //获取集合类型的枚举器
    NSEnumerator *enumrator = [[manage subpathsAtPath:path] objectEnumerator];
    //每次遍历得到的文件名
    NSString *fileName = [NSString string];
    //文件夹大小
    float folderSize = 0;
    while ((fileName = [enumrator nextObject]) != nil) {
        NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:absolutePath];
    }
    
    return folderSize / (1024.0 * 1024.0);
    
}


- (void)clearButton:(UIButton *)sender {
    
    _clearLabel.text = [CleanCaches cleanCachesAtPath];

    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
